const MYSQL = require("../../sql/mysql");
const Dayjs = require("dayjs");
const SQLName = "feedback";

const Config = require("../../config");
/**
 * 反馈类目列表
 * @param {*} params
 */
async function FeedbackTypeList(Request) {
  try {
    let Query = Request.query;
    let user = Request.$user;
    let SqlName = "feedback_type";
    let { page, limit } = Query;
    // 获取Mysql 连接
    limit = limit < 1 ? (limit = 1) : 10;
    page = page < 1 ? (page = 1) : 1;
    const Conline = await MYSQL.promise().getConnection();
    let [list] = await Conline.query(`
      SELECT * FROM ${SqlName}
      LIMIT ${(page - 1) * limit + "," + page * limit}
    `);
    // 查询完毕，释放池连接回池
    await Conline.release();
    return {
      code: 0,
      msg: "操作成功",
      data: {
        page,
        limit,
        list,
      },
    };
  } catch (error) {
    console.log(error);
    throw {
      code: 500,
      msg: "操作失败",
    };
  }
}
/**
 * 删除类目
 * @param {*} params
 */
async function DeleteFeedbackType(Request) {
  try {
    let Body = Request.body;
    let { id } = Body;
    if (!id) {
      throw { code: 500, msg: "删除失败,id不能为空" };
    }
    let SqlName = "feedback_type";
    // 获取Mysql连接
    const Conline = await MYSQL.promise().getConnection();
    let [UpdateResult] = await Conline.query(
      `UPDATE ${SqlName} SET ? WHERE id = ${id}`,
      {
        state: 0,
      }
    );
    // 查询完毕，释放池连接回池
    await Conline.release();
    let {affectedRows} = UpdateResult
    if(affectedRows){
      return {
        code: 0,
        msg: "删除成功",
      };
    }else{
      throw {
        code:500,msg:"删除失败,记录不存在"
      }
    }
  } catch (error) {
    let { msg } = error;
    if (msg) {
      throw error;
    } else {
      throw { code: 500, msg: "删除失败" };
    }
  }
}
/**
 * 创建类目
 * @param {*} params
 */
async function CreateFeedbackType(Request) {
  try {
    let Body = Request.body;
    let user = Request.$user;
    let { name } = Body;
    if (!name) {
      throw {
        code: 500,
        msg: "缺少name字段",
      };
    }
    let Inset = {
      name,
      created_date: Dayjs(Date.now()).valueOf(),
      create_user: user,
    };
    let SqlName = "feedback_type";
    const Conline = await MYSQL.promise().getConnection();
    // 查询是否存在相同name的数据
    let [CountResult] = await Conline.query(
      `SELECT Count(*) AS Count FROM ${SqlName} WHERE name = ? AND state = ?`,
      [name, 1]
    );
    let { Count } = CountResult[0];
    if (Count) {
      throw {
        code: 500,
        msg: "添加失败,已存在相同的name",
      };
    }
    let [InsertResult] = await Conline.query(
      `INSERT INTO ${SqlName} SET ?`,
      Inset
    );
    // 查询完毕，释放池连接回池
    await Conline.release();
    let { affectedRows } = InsertResult;
    if (affectedRows) {
      return {
        code: 0,
        msg: "操作成功",
      };
    } else {
      throw {
        code: 500,
        msg: "操作失败,未知错误",
      };
    }
  } catch (error) {
    let { msg } = error;
    if (msg) {
      throw error;
    } else {
      throw { code: 500, msg: "操作失败" };
    }
  }
}
/**
 * 修改类目
 * @param {*} params
 */
async function UpdateFeedbackType(Request) {}
/**
 * 反馈记录列表
 * @param {*} params
 */
async function FeedbackList(Request) {
  try {
    let Query = Request.query;
    let { page = 1, limit = 10 } = Query;
    limit = limit < 1 ? (limit = 1) : limit;
    page = page < 1 ? (page = 1) : page;
    const Conline = await MYSQL.promise().getConnection();
    let [list] = await Conline.query(`
      SELECT * FROM ${SQLName}
      LIMIT ${(page - 1) * limit + "," + page * limit}
    `);
    // 获取数据总数 =====
    let [totalResult] = await Conline.query(
      `
        SELECT Count(*) AS total FROM ${SQLName} 
      `
    );
    // 查询完毕，释放池连接回池
    await Conline.release();
    total = totalResult[0].total;
    pages = Math.ceil(total / limit);
    return {
      code: 0,
      msg: "获取成功",
      data: {
        page,
        limit,
        total,
        list,
      },
    };
  } catch (error) {
    console.log(error);
    throw { code: 500, msg: "获取失败" };
  }
}
/**
 * 我的反馈记录列表
 * @param {*} params
 */
async function GetMyFeedbackList(Request) {}
/**
 * 新增反馈记录
 * @param {*} params
 */
async function addFeedback(Request) {
  try {
    let user = Request.$user;

    let Body = Request.body;

    let { title, value, type } = Body;

    let feedbackConfig = Config.feedback;

    let { status, login } = feedbackConfig;

    // 是否关闭反馈功能
    if (!status) {
      throw { code: 500, msg: "添加失败，反馈已关闭" };
    }
    // 反馈是否需要登录
    if (login && !user) {
      throw { code: 500, msg: "添加失败，user不能为空" };
    }
    // 反馈参数判断
    if (!type) {
      throw { code: 500, msg: "添加失败，type不能为空" };
    } else if (!title) {
      throw { code: 500, msg: "添加失败，title不能为空" };
    } else if (!value) {
      throw { code: 500, msg: "添加失败，value不能为空" };
    }
    // 当前时间
    let create_date = Dayjs(Date.now()).valueOf();

    // 执行MYSQL
    const Conline = await MYSQL.promise().getConnection();

    let result = await Conline.query(`INSERT INTO ${SQLName} SET ?`, {
      type,
      title,
      value,
      create_date,
    });

    let insertResult = result[0].affectedRows;

    if (insertResult) {
      return {
        code: 0,
        msg: "添加成功",
      };
    } else {
      throw {
        code: 500,
        msg: "添加失败,未知错误",
      };
    }
  } catch (error) {
    throw error;
  }
}
/**
 * 删除反馈记录
 * @param {*} params
 */
async function DeleteFeedback(Request) {
  try {
    let Body = Request.body;
    let { id } = Body;
    if (!id) {
      throw { code: 500, msg: "删除失败,id不能为空" };
    }
    const Conline = await MYSQL.promise().getConnection();
    let [affectedRows] = await Conline.query(`UPDATE ${SQLName} SET ?`, {
      state: 0,
    });
    // 查询完毕，释放池连接回池
    await Conline.release();
    if (affectedRows) {
      return {
        code: 0,
        msg: "删除成功",
      };
    } else {
      throw { code: 500, msg: "删除失败,记录不存在" };
    }
  } catch (error) {
    let { msg } = error;
    if (msg) {
      throw error;
    } else {
      throw { code: 500, msg: "操作失败" };
    }
  }
}
/**
 * 查看反馈信息
 */
async function GetFeedbackInfo(Request) {
  try {
    let { id } = params;
    if (!id) {
      throw { code: 500, msg: "删除失败,id不能为空" };
    }
    let feedbackInfo = await MYSQL.promise().query(
      `SELECT * FROM ${SQLName} WHERE id = ${id}`
    );
    return {
      code: 0,
    };
  } catch (error) {}
}
/**
 * 回复反馈信息
 */
async function ReplyFeedback(Request) {}

module.exports = {
  FeedbackTypeList,
  DeleteFeedbackType,
  CreateFeedbackType,
  UpdateFeedbackType,
  DeleteFeedback,
  GetFeedbackInfo,
  ReplyFeedback,
  FeedbackList,
  addFeedback,
};
